import FormHook from "@/hook/formHook";
import { cloneDeep } from "lodash";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formDataRef, formState, emit } = state;
  const { resetFields } = FormHook({ formState });
  const onFinish = () => {
    emit("submit", cloneDeep(formState));
  };
  const cancelHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const changeUnitHandle = () => {
    if (Number(formState.unit) === 1) formState.value = 1;
    else formState.value = undefined as unknown as number;
  };
  return {
    onFinish,
    cancelHandle,
    resetFields,
    changeUnitHandle
  };
}
